#include "project.h"
#include "../helper.h"
#include <core/config/configfile.h>

#include "projectitem.h"
#include "projectmodelitem.h"
#include "common.h"

class ProjectPrivate{
public:
	ProjectPrivate():
			treeView(new QTreeView)
			,model(0)
	{
	}
	~ProjectPrivate()
	{
		delete treeView;
	}

	QList<QFileInfo> files;
	Project::ProjectType type;
	QList<ProjectItem*> items;
	QString projectName;
	QTreeView* treeView;
	ProjectModelItem* model;
};

Project::Project(ProjectItem *parent) :
		ProjectFile(parent), O_PRIVATE_CREATE(Project)
{
	O_D(Project);
	//d->
}

Project::~Project()
{
	unload();
	O_PRIVATE_DELETE;
}

Project::ProjectType Project::type()
{
	O_D(Project);
	return d->type;
}

void Project::unload()
{
	O_D(Project);
	d->files.clear();
	d->treeView->setModel(0);
}

ProjectItem* Project::addItem(ProjectItem* item, ProjectItem* parent)
{
    Q_ASSERT(item);
    O_D(Project);
	return ProjectItem::addItem(item);
    #if 0
if (parent == 0)
    {
        d->items.append(item);
        return item;
    }

    QObject* p = 0;
    foreach (ProjectItem* it, d->items)
    {
        p = item->parent();
        while (p != 0 && p != parent)
        {
            p = p->parent();
        }

        if (p != 0)
            break;
    }
    if (p == 0)
    {
        LIW << "Parent:" << qobject_cast<Opl::Core::EntityObject*>(p)->toString();
        return 0;
    }

    item->setParent(p);
#endif

   // return item;
}

bool Project::save()
{
	O_D(Project);
	Opl::Core::ConfigFile* projectFile = new Opl::Core::ConfigFile(source().toLocalFile());
	//projectFile.setFileName(source().toLocalFile());
	projectFile->setValue("Project", "data", serialize());
	projectFile->save();
	delete projectFile;
	return true;
}

bool Project::load()
{
	//d->files = info().absoluteDir().entryInfoList(QDir::Files);
	O_D(Project);
	Opl::Core::ConfigFile projectFile;
	projectFile.setFileName(source().toLocalFile());	
	if (!projectFile.load())
		return false;

	QVariantMap m = projectFile.value("Project", "data", "").toMap();
	deserialize(m);

	d->model = new ProjectModelItem(this);
	d->treeView->setModel(d->model);
	setStatus(s_loaded);
}

bool Project::saveAs(const QUrl& url)
{
	setSource(url);
	return save();
}

bool Project::create()
{
	O_D(Project);
	ProjectFile::create();
	return true;
}

QWidget* Project::widget()
{
	O_D(Project);
	return d->treeView;
}

QVariantMap Project::serialize()
{
	O_D(Project);
	QVariantList lst;
	foreach(ProjectItem* i, items())
	{
		QVariantMap m = i->serialize();
		lst.append(m);
	}

	QVariantMap map = ProjectFile::serialize();
	map.insert("ProjectItems", lst);
	return map;
}

void Project::deserialize( const QVariantMap& m )
{
	O_D(Project);
	ProjectFile::deserialize(m);
	QVariantList lst = m["ProjectItems"].toList();
	foreach(QVariant v, lst)
	{
		QVariantMap map = v.toMap();
		QString t = map["type"].toString();
		ProjectItem* i = fromItemType(t);
		if (i != 0)
		{
			i->deserialize(map);
			addItem(i);
		}
	}
}



